package com.cxl.demo.awareness;

import android.app.PendingIntent;
import android.content.Context;

import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hms.kit.awareness.Awareness;
import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

import java.util.List;

public class BarrierUtils {

    public static void addBarrier(Context context, final String label, AwarenessBarrier barrier, PendingIntent pendingIntent, BarrierCallback callback) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request = builder.addBarrier(label, barrier, pendingIntent).build();
        Awareness.getBarrierClient(context).updateBarriers(request)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        if (callback != null) {
                            callback.onFailure();
                        }
                    }
                });
    }

    public static void addBatchBarrier(Context context, List<BarrierEntity> barrierList, BarrierCallback callback) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        for (BarrierEntity entity : barrierList) {
            builder.addBarrier(entity.getBarrierLabel(), entity.getBarrier(), entity.getPendingIntent());
        }
        Awareness.getBarrierClient(context).updateBarriers(builder.build())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        if (callback != null) {
                            callback.onFailure();
                        }
                    }
                });
    }

    public static void deleteBarrier(Context context, BarrierCallback callback, PendingIntent... pendingIntents) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        for (PendingIntent pendingIntent : pendingIntents) {
            builder.deleteBarrier(pendingIntent);
        }
        Awareness.getBarrierClient(context).updateBarriers(builder.build())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        if (callback != null) {
                            callback.onFailure();
                        }
                    }
                });
    }

    public static void deleteBarrier(Context context, BarrierCallback callback, String... labels) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        for (String label : labels) {
            builder.deleteBarrier(label);
        }
        Awareness.getBarrierClient(context).updateBarriers(builder.build())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        if (callback != null) {
                            callback.onSuccess();
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        if (callback != null) {
                            callback.onFailure();
                        }
                    }
                });
    }
}
